/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.rm.entidades.jpa;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;

import org.rm.entidades.ExperienciaBase;
import org.rm.entidades.jpa.exceptions.NonexistentEntityException;

/**
 *
 * @author ffbeltran
 */
public class JpaExperiencia {

    public static void create(ExperienciaBase experiencia) {
        EntityManager em = null;
        try {
            em = JpaController.getEntityManager();
            em.getTransaction().begin();
            em.persist(experiencia);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public static void edit(ExperienciaBase experiencia) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = JpaController.getEntityManager();
            em.getTransaction().begin();
            experiencia = em.merge(experiencia);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = experiencia.getId();
                if (findExperienciaBase(experiencia.getClass(), id) == null) {
                    throw new NonexistentEntityException("La experiencia "
                            + experiencia.getClass().getSimpleName() +" con id "
                            + id + " no existe.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public static void destroy(Class clazz, Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = JpaController.getEntityManager();
            em.getTransaction().begin();
            ExperienciaBase exp;
            try {
                exp = (ExperienciaBase) em.getReference(clazz, id);
                exp.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("La experiencia "
                            + clazz.getSimpleName() +" con id "
                            + id + " no existe.", enfe);
            }
            em.remove(exp);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public static <T extends ExperienciaBase> List<T> findExperienciaBaseEntities(Class<T> clazz) {
        return findExperienciaBaseEntities(clazz, true, -1, -1);
    }

    public static <T extends ExperienciaBase> List<T> findExperienciaBaseEntities(Class<T> clazz, int maxResults, int firstResult) {
        return findExperienciaBaseEntities(clazz, false, maxResults, firstResult);
    }

    private static <T extends ExperienciaBase> List<T> findExperienciaBaseEntities(Class<T> clazz, boolean all, int maxResults, int firstResult) {
        EntityManager em = JpaController.getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from "+clazz.getSimpleName()+" as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public static <T extends ExperienciaBase> List<T> findExperienciaBaseEntities(Class<T> clazz, int idPartida) {
        EntityManager em = JpaController.getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from "+clazz.getSimpleName()+" as o where o.idPartida = :idP");
            q.setParameter("idP", idPartida);
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public static <T extends ExperienciaBase> T findExperienciaBase(Class<T> clazz, Integer id) {
        EntityManager em = JpaController.getEntityManager();
        try {
            return em.find(clazz, id);
        } finally {
            em.close();
        }
    }

    public static <T extends ExperienciaBase> int getExperienciaBaseCount(Class<T> clazz) {
        EntityManager em = JpaController.getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from "+clazz.getSimpleName()+" as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
